package com.wxl.sharding.table;

import com.google.common.collect.Range;
import com.wxl.util.SnowflakeIdWorker;
import lombok.Generated;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Collection;
import java.util.Properties;

public class MyTableShardingAlgo implements StandardShardingAlgorithm<Long> {


    private Properties props = new Properties();


    // 分表名前缀（格式：t_order_0, t_order_1）
    private String tabPrefix = "t_order_";

    private int sharding = 2;


    @Override
    public String doSharding(Collection<String> availableTargetNames,
                             PreciseShardingValue<Long> preciseShardingValue) {
        Long orderId = preciseShardingValue.getValue();


        // 2. 构造分库名称
        String targetTab = tabPrefix + orderId % sharding;

        // 3. 验证分库是否存在
        if (availableTargetNames.contains(targetTab)) {
            System.out.println("找到表："+targetTab);
            return targetTab;
        }

        throw new IllegalArgumentException("无效的分表: " + targetTab + " 可用分表: " + availableTargetNames);
    }

    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Long> rangeShardingValue) {

        DataNodeInfo dataNodeInfo = rangeShardingValue.getDataNodeInfo();
        String columnName = rangeShardingValue.getColumnName();
        String logicTableName = rangeShardingValue.getLogicTableName();
        Range<Long> valueRange = rangeShardingValue.getValueRange();
        Long aLong = valueRange.upperEndpoint();
        Long aLong1 = valueRange.lowerEndpoint();


        return collection;
    }



    @Override
    public String getType() {
        return "MY_TABLE_MOD";
    }

    @Override
    public void init(Properties properties) {
        this.props = properties;
    }

    @Generated
    public Properties getProps() {
        return this.props;
    }


}
